package com.course.finalproject.adapter;

import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.course.finalproject.Constants;
import com.course.finalproject.R;
import com.course.finalproject.model.BatteryLog;

/**
 * Specific array adapter for battery logs ListView.
 * @author	Mariano Bessone <mariano.bessone @ tallertechnologies.com>
 * @since	2012-09-18
 */
public class BatteryLogArrayAdapter extends ArrayAdapter<BatteryLog> {

	/**	Context */
	private final Activity context;
	/**	Array of battery logs. */
	private List<BatteryLog> batteryLogs;

	/**
	 * ViewHolder to optimize the number of layout inflations.
	 * @author	Mariano Bessone <mariano.bessone @ tallertechnologies.com>
	 * @since	2012-09-18
	 */
	static class ViewHolder {
		public TextView date;
		public TextView time;
		public TextView levelScale;
		public TextView percentage;
	}

	/**
	 * Constructor.
	 * 
	 * @param context Context to get the layout inflater.
	 * @param batteryLogs Initial list of BatteryLog elements.
	 */
	public BatteryLogArrayAdapter(Activity context, List<BatteryLog> batteryLogs) {
		super(context, R.layout.battery_list_item, batteryLogs);
		this.context = context;
		this.batteryLogs = batteryLogs;
	}

	/**
	 * Gets a View from the ListView to show it.
	 * 
	 * @param position The position in the ListView of the View to get.
	 * @param convertView The old View in the same position or null if it is the first time.
	 * @param parent The view group.
	 * @return The View to show.
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.battery_list_item, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.date = (TextView) rowView.findViewById(R.id.battery_log_date);
			viewHolder.time = (TextView) rowView.findViewById(R.id.battery_log_time);
			viewHolder.levelScale = (TextView) rowView.findViewById(R.id.battery_log_level_scale);
			//viewHolder.scale = (TextView) rowView.findViewById(R.id.battery_log_scale);
			viewHolder.percentage = (TextView) rowView.findViewById(R.id.battery_log_percentage);
			rowView.setTag(viewHolder);
		}
		// Set the corresponding text's.
		ViewHolder holder = (ViewHolder) rowView.getTag();
		holder.date.setText( batteryLogs.get(position).getDate() );
		holder.time.setText( batteryLogs.get(position).getTime() );
		holder.levelScale.setText( "Lvl: " + batteryLogs.get(position).getLevel() + "/" + batteryLogs.get(position).getScale() );
		holder.percentage.setText( batteryLogs.get(position).getBatteryPercentage() + "%" );
		// Set background color depending if the position is odd or even.
		if (position % 2 == 0)
			rowView.setBackgroundColor(Color.parseColor(Constants.COLOR_BATTERY_LOG_CONTRAST) );
		else
			rowView.setBackgroundColor(Color.WHITE);
		// Return the row view.
		return rowView;
	}

}
